package com.smartlifev30.baiwei.view.dialog;

import android.app.Dialog;
import android.content.Context;
import android.content.DialogInterface;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.PaintDrawable;
import android.graphics.drawable.StateListDrawable;
import android.os.Build;
import android.os.Bundle;
import android.os.Handler;
import android.os.Looper;
import android.support.annotation.ColorInt;
import android.support.annotation.DrawableRes;
import android.support.annotation.IdRes;
import android.support.annotation.IntegerRes;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.view.Window;
import android.view.WindowManager;
import android.widget.ImageView;
import android.widget.TextView;

import com.smartlifev30.R;

/**
 * @author uesr
 * @date 2018/7/31
 */

public class NotifyTipsDialog extends Dialog implements DialogInterface.OnShowListener, View.OnClickListener, DialogInterface.OnDismissListener {

    /**
     * 延迟时间dismiss,注意延迟时间后和动画的配合
     */
    private int defyDismissTime;

    /**
     * 主题
     */
    private String them;

    /**
     * 内容
     */
    private String content;

    /**
     * 图片id
     */
    @DrawableRes
    private int iconResId;

    /**
     * 点击事件
     */
    private NotifyClickListen onNotifyClickListen;

    /**
     * 点击颜色
     */
    @ColorInt
    private int pressedColor;

    /**
     * 正常颜色
     */
    @ColorInt
    private int norColor;
    private TextView themTv;
    private TextView contentTv;
    private ImageView iconIv;

    NotifyTipsDialog(Builder builder) {
        super(builder.context, R.style.dialog_transparent);
        this.defyDismissTime = builder.defyDismissTime;
        this.them = builder.them;
        this.content = builder.content;
        this.iconResId = builder.iconResId;
        this.onNotifyClickListen = builder.onNotifyClickListen;
        this.norColor = builder.norColor;
        this.pressedColor = builder.pressedColor;
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.notify_dialog);
        setCanceledOnTouchOutside(true);
        setOnShowListener(this);
        setOnDismissListener(this);
        Window window = getWindow();
        if (window != null) {
            WindowManager.LayoutParams attributes = window.getAttributes();
            attributes.width = WindowManager.LayoutParams.MATCH_PARENT;
            attributes.height = WindowManager.LayoutParams.WRAP_CONTENT;
            attributes.gravity = Gravity.CENTER_HORIZONTAL | Gravity.TOP;
            attributes.y = 30;
            attributes.dimAmount = 0.6f;
            attributes.windowAnimations = R.style.notify_dialog_anim;
            window.setAttributes(attributes);
            initView();
        }
    }

    private void initView() {
        themTv = findViewById(R.id.notify_them);
        contentTv = findViewById(R.id.notify_context);
        iconIv = findViewById(R.id.notify_icon);

        View view = findViewById(R.id.notify);

        view.setOnClickListener(this);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            view.setBackground(getBackGroundDrawable());
        } else {
            view.setBackgroundDrawable(getBackGroundDrawable());
        }

    }


    /**
     * 背景Drawable
     *
     * @return
     */
    private Drawable getBackGroundDrawable() {
        StateListDrawable drawable = new StateListDrawable();
        PaintDrawable pressedDrawable = new PaintDrawable(pressedColor);
        PaintDrawable norDrawable = new PaintDrawable(norColor);
        float v = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 5,
                getContext().getResources().getDisplayMetrics());
        pressedDrawable.setCornerRadius(v);
        norDrawable.setCornerRadius(v);
        drawable.addState(new int[]{android.R.attr.state_pressed}, pressedDrawable);
        drawable.addState(new int[]{}, norDrawable);
        return drawable;
    }


    @Override
    public void show() {
        super.show();
        themTv.setText(them);
        contentTv.setText(content);
        iconIv.setImageResource(iconResId);
    }


    @Override
    public void onShow(DialogInterface dialog) {
        if (defyDismissTime > 0) {
            new Handler(Looper.getMainLooper()).postDelayed(() -> {
                if (isShowing()) {
                    dismiss();
                }
            }, defyDismissTime);
        }
    }

    @Override
    public void onDismiss(DialogInterface dialog) {

    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.notify:
                if (onNotifyClickListen != null) {
                    onNotifyClickListen.onClickListen(this);
                    dismiss();
                }
                break;
            default:
        }
    }

    public String getThem() {
        return them;
    }

    public String getContent() {
        return content;
    }

    public void setThem(String them) {
        this.them = them;
    }

    public void setContent(String content) {
        this.content = content;
    }

    public void setDefyDismissTime(int defyDismissTime) {
        this.defyDismissTime = defyDismissTime;
    }

    public static class Builder {
        private int defyDismissTime;
        private Context context;
        private String them;
        private String content;
        private int iconResId = R.mipmap.app_icon;
        private NotifyClickListen onNotifyClickListen;
        private int norColor;
        private int pressedColor;

        public Builder(Context context) {
            this.context = context;
        }


        /**
         * 延迟多长时间后dismisses，注意和动画时间的配合
         *
         * @param defyDismissTime
         * @return
         */
        public Builder delayDismissTime(int defyDismissTime) {
            this.defyDismissTime = defyDismissTime;
            return this;
        }

        /**
         * 主题
         *
         * @param them
         * @return
         */
        public Builder them(String them) {
            this.them = them;
            return this;
        }

        /**
         * 图片id
         *
         * @param iconResId
         * @return
         */
        public Builder iconResId(@DrawableRes int iconResId) {
            this.iconResId = iconResId;
            return this;
        }

        /**
         * 內容
         *
         * @param content
         * @return
         */
        public Builder content(String content) {
            this.content = content;
            return this;
        }

        /**
         * 点击事件
         *
         * @param listen
         * @return
         */
        public Builder notifyClickListen(NotifyClickListen listen) {
            this.onNotifyClickListen = listen;
            return this;
        }

        /**
         * 点击颜色
         *
         * @param color
         * @return
         */
        public Builder pressedColor(@ColorInt int color) {
            this.pressedColor = color;
            return this;
        }


        /**
         * 正常顏色
         *
         * @param color
         * @return
         */
        public Builder norColor(@ColorInt int color) {
            this.norColor = color;
            return this;
        }

        public NotifyTipsDialog build() {
            if (pressedColor <= 0) {
                pressedColor = Color.parseColor("#66000000");
            }
            if (norColor <= 0) {
                norColor = Color.parseColor("#30000000");
            }
            return new NotifyTipsDialog(this);
        }

    }

    /**
     * 通知点击事件接口
     */
    public interface NotifyClickListen {
        /**
         * 通知点击事件
         *
         * @param notifyTips
         */
        void onClickListen(NotifyTipsDialog notifyTips);
    }
}


